package com.erp.erp_ui.warehouse.controller;

import com.alibaba.fastjson.JSONObject;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.warehouse.req.ImportCarryPlanReq;
import com.erp.erp_entitys.warehouse.req.ImportPurchaseReq;
import com.erp.erp_entitys.warehouse.req.OtherInStockPageListInfoReq;
import com.erp.erp_entitys.warehouse.req.sellOutStockStatistics1Req;
import com.erp.erp_ui.base.Base;
import com.erp.erp_ui.money.req.OperationReq;
import com.erp.erp_ui.warehouse.req.AddOtherInStockReq;
import com.erp.erp_ui.warehouse.service.OtherInStockService;
import com.erp.erp_ui.warehouse.vo.*;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName : OtherInStockController
 * @Description : 其它入库控制层
 * @Author : lst
 * @Date: 2021-03-24 18:34
 */
@Controller
@RequestMapping(value = "otherInStock")
public class OtherInStockController extends Base {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(OtherInStockController.class);

    /**
     * 其它入库业务逻辑层接口
     */
    private final OtherInStockService otherInStockService;

    @Autowired
    public OtherInStockController(OtherInStockService otherInStockService) {
        this.otherInStockService = otherInStockService;
    }

    /**
     * 首页-获取分页信息
     * @param req 查询条件实体
     * @return
     */
    @GetMapping("/getListByPage")
    @ResponseBody
    public R getListByPage(OtherInStockPageListInfoReq req){
        try {
            LOGGER.info("其它入库-首页-获取分页信息:" + req.toString());
            PageInfo<OtherInStockPageListVo> pageInfo = otherInStockService.getListByPage(req);
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            LOGGER.info("其它入库-首页-获取分页信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 新增-导采购信息
     * @param req 查询条件
     * @return 采购信息
     */
    @GetMapping(value = "import/purchaseByPage")
    @ResponseBody
    public R getImportPurchaseInfo(ImportPurchaseReq req){
        try {
            LOGGER.info("其它入库-新增-导采购信息:" + req.toString());
            PageInfo<ImportPurchaseVo> pageInfo = otherInStockService.getImportPurchaseInfo(req);
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            LOGGER.info("其它入库-新增-导采购信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 新增-导采购产品信息
     * @param id 采购id
     * @return 采购产品信息
     */
    @GetMapping(value = "import/purchaseProduct")
    @ResponseBody
    public R getImportPurchaseProduct(Integer id){
        try {
            LOGGER.info("其它入库-新增-导采购产品信息: id=" + id);
            List<ImportPurchaseProductVo> productList = otherInStockService.getImportPurchaseProduct(id);
            return R.build(productList);
        }catch (Exception e){
            LOGGER.info("其它入库-新增-导采购产品信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 新增-导出运计划信息
     * @param req 查询条件
     * @return 出运计划信息
     */
    @GetMapping(value = "import/carryPlanByPage")
    @ResponseBody
    public R getImportCarryPlan(ImportCarryPlanReq req){
        try {
            LOGGER.info("其它入库-新增-导出运计划信息:" + req.toString());
            PageInfo<ImportCarryPlanVo> pageInfo = otherInStockService.getImportCarryPlan(req);
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            LOGGER.info("其它入库-新增-导出运计划信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 新增-导出运计划产品信息
     * @param id 出运计划id
     * @return 出运计划产品信息
     */
    @GetMapping(value = "import/carryPlanProduct")
    @ResponseBody
    public R getImportCarryPlanProduct(Integer id){
        try {
            LOGGER.info("其它入库-新增-导出运计划产品信息: id=" + id);
            List<ImportCarryPlanProductVo> productList = otherInStockService.getImportCarryPlanProduct(id);
            return R.build(productList);
        }catch (Exception e){
            LOGGER.info("其它入库-新增-导出运计划产品信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 其它入库入库保存
     * @param req 新增数据实体
     * @return 新增结果
     */
    @PostMapping("/add")
    @ResponseBody
    public R add(@Validated @RequestBody AddOtherInStockReq req){
        try {
            LOGGER.info("仓库管理-其它入库-新增 req=" + req.toString());
            UserEntity user = (UserEntity)this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return otherInStockService.add(req, name, code);
        }catch (Exception e){
            LOGGER.info("仓库管理-其它入库-新增异常：" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取详情
     * @param id 其它入库id
     * @return
     */
    @GetMapping("/detail")
    @ResponseBody
    public R getDetailInfo(Integer id){
        try {
            LOGGER.info("仓库管理-其它入库-获取详情 id=" + id);
            OtherInStockInfoVo vo = otherInStockService.getDetailInfo(id);
            return R.build(vo);
        } catch (Exception e){
            LOGGER.info("仓库管理-其它入库-获取详情异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 删除
     * @param req 删除条件
     * @return 删除结果
     */
    @PostMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody OperationReq req) {
        try {
            LOGGER.info("仓库管理-其它入库-删除 req=" + req);
            otherInStockService.delete(req);
            return R.build("");
        } catch (Exception e) {
            LOGGER.info("仓库管理-其它入库-删除异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

    /**
     * 数据操作接口
     * @param req 操作对象
     * @return 结果
     */
    @PostMapping("/operation")
    @ResponseBody
    public R operation(@RequestBody OperationReq req) {
        try {
            LOGGER.info("仓库管理-其它入库-数据操作接口 req=" + req);
            UserEntity user = (UserEntity)this.getBean("user");
            String msn = user.getMsn();
            return otherInStockService.operation(req, msn);
        } catch (Exception e) {
            LOGGER.info("仓库管理-其它入库-数据操作接口异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 其它入库产品列表统计--分页
     *
     * @param req 操作对象
     * @return 操作结果
     */
    @RequestMapping("/statistics/productInfo")
    @ResponseBody
    public R statisticsProductInfo(sellOutStockStatistics1Req req) {
        try {
            LOGGER.info("仓库管理-其它入库-其它出库产品列表统计 req=" + req);
            PageInfo<OtherInStockProductStatisticsVo> pageInfo = otherInStockService.statisticsProductInfo(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("仓库管理-其它入库-其它出库产品列表统计异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 其它入库产品列表统计-导出
     *
     * @param req 操作对象
     * @return 操作结果
     */
    @RequestMapping("/export/productInfoExport")
    @ResponseBody
    public R statisticsProductInfoExport(sellOutStockStatistics1Req req) {
        try {
            LOGGER.info("仓库管理-其它入库-其它出库产品列表统计导出 req=" + req);
            JSONObject info = otherInStockService.statisticsProductInfoExport(req);
            return R.build(info);
        } catch (Exception e) {
            LOGGER.info("仓库管理-其它入库-其它出库产品列表统计导出异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


}
